package com.yupi.yupicturebackend.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.crypto.digest.MD5;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.server.HttpServerRequest;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.yupi.yupicturebackend.common.UserHolder;
import com.yupi.yupicturebackend.exception.BusinessException;
import com.yupi.yupicturebackend.exception.ErrorCode;
import com.yupi.yupicturebackend.model.entity.User;
import com.yupi.yupicturebackend.mapper.UserMapper;
import com.yupi.yupicturebackend.model.enums.UserRoleEnum;
import com.yupi.yupicturebackend.model.vo.UserVO;
import com.yupi.yupicturebackend.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Random;
import java.util.UUID;


/**
 * @author Administrator
 * @description 针对表【user(用户)】的数据库操作Service实现
 * @createDate 2025-04-14 16:16:43
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService {

    @Override
    public Long userRegister(String userAccount, String userPassword, String checkPassword) {
        //1.校验
        if (StrUtil.hasBlank(userAccount, userAccount, checkPassword)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数错误");
        }
        if (userAccount.length() < 4 || userAccount.length() > 16) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "账号长度小于4位或大于16为");
        }
        if (userPassword.length() < 8 || userPassword.length() > 16) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "密码不符合规则");
        }
        if (!userPassword.equals(checkPassword)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "两次密码不一样");
        }
        //2.检查是否重复
        User user1 = query().eq("userAccount", userAccount).one();
        if (user1 != null) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "用户已经存在");
        }
        //3.加密
        String newPassword = DigestUtil.md5Hex(userPassword);
        //4.插入数据
        User user = new User();
        user.setUserAccount(userAccount);
        user.setUserPassword(newPassword);
        user.setUserRole(UserRoleEnum.USER.toString());
        user.setUserName("图友" + RandomUtil.randomString(6));
        boolean save = this.save(user);
        if (save == false) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "数据库异常");
        }
        //5.返回
        return user.getId();
    }

    @Override
    public UserVO userLogin(String userAccount, String userPassword, HttpServletRequest httpRequest) {
        //1.校验用户信息是否正确
        if (StrUtil.hasBlank(userAccount, userPassword)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数为空");
        }
        if (userAccount.length() <= 3 || userAccount.length() > 16) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "账号不合法");
        }
        if (userPassword.length() < 8 || userPassword.length() > 16) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "密码不合法");
        }
        //2.数据库查找用户信息
        User user = query().eq("userAccount", userAccount).one();
        if (user == null) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "用户不存在");
        }
        //3.密码比对
        String newpassword = DigestUtil.md5Hex(userPassword);
        if (!newpassword.equals(user.getUserPassword())) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "密码错误");
        }
        //4.存储到session
        UserVO userVO = new UserVO();
        BeanUtil.copyProperties(user, userVO, false);
        httpRequest.getSession().setAttribute("user_login", userVO);
        //4.1用户信息存储到threadlocal
        log.info("当前用户{}",userVO);
        UserHolder.saveUser(userVO);
        //5.返回脱敏用户信息
        return userVO;
    }

    @Override
    public UserVO getUser(HttpServletRequest httpRequset) {
        UserVO userLogin = (UserVO) httpRequset.getSession().getAttribute("user_login");
        if (userLogin == null) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "当前用户未登录");
        }
        return userLogin;
    }

    @Override
    public boolean isADMIN(User user) {
        return user != null && UserRoleEnum.ADMIN.getValue().equals(user.getUserRole());
    }

}




